package com.oracle.zebrafreelytraveling.ui;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.oracle.zebrafreelytraveling.R;
import com.oracle.zebrafreelytraveling.data.bean.User;
import com.oracle.zebrafreelytraveling.net.HttpManager;
import com.oracle.zebrafreelytraveling.ui.fragment.GetImgDialogFragment;
import com.oracle.zebrafreelytraveling.utils.constant.Constant;
import com.oracle.zebrafreelytraveling.utils.StringUtil;
import com.oracle.zebrafreelytraveling.utils.ToastUtil;
import com.oracle.zebrafreelytraveling.utils.UserUtil;

import org.litepal.crud.DataSupport;

/**
 * 个人中心界面
 */
public class PersonalCenterActivity extends BaseActitvity implements View.OnClickListener, GetImgDialogFragment.OnCurrentPathListener {


    private static final String USER_INFO = "user_info";
    private static final String USER_TYPE = "user_type";

    public static final int PERSONAL = 0;
    public static final int OTHER = 1;

    private SimpleDraweeView mPersonalBg;
    private SimpleDraweeView mPersonalHeader;
    private SimpleDraweeView mPersonalQrCode;
    private TextView mPersonalName;
    private TextView mPersonalAccount;
    private TextView mPersonalSexAndAge;
    private TextView mPersonalEmail;
    private TextView mPersonalLove;
    private Button mEdPersonalInfo;

    //界面显示的类型
    private int viewType;
    private User otherUser;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_center);
        getIntentData();
        initView();
        initPersonalInfo();
    }

    private void getIntentData() {
        Intent intent = getIntent();
        viewType = intent.getIntExtra(USER_TYPE, -1);
        if (viewType == OTHER) {
            otherUser = (User) intent.getSerializableExtra(USER_INFO);
        }
    }

    private void initView() {

        mPersonalBg = (SimpleDraweeView) findViewById(R.id.personal_bg);
        mPersonalHeader = (SimpleDraweeView) findViewById(R.id.personal_header);
        mPersonalQrCode = (SimpleDraweeView) findViewById(R.id.personal_qr_code);
        mPersonalName = (TextView) findViewById(R.id.personal_name);
        mPersonalAccount = (TextView) findViewById(R.id.personal_account);
        mPersonalSexAndAge = (TextView) findViewById(R.id.personal_sex_age);
        mPersonalEmail = (TextView) findViewById(R.id.personal_email);
        mPersonalLove = (TextView) findViewById(R.id.personal_love);
        mEdPersonalInfo = (Button) findViewById(R.id.ed_personal_info);

        mPersonalBg.setOnLongClickListener(v -> {
            GetImgDialogFragment.newInstance().showDialogFragment(getFragmentManager());
            return true;
        });
        mPersonalQrCode.setOnClickListener(this);
        mEdPersonalInfo.setOnClickListener(this);
    }


    private void initPersonalInfo() {
        //TODO 从个人信息获取相关信息  先设置默认信息
        User user = null;
        if (viewType == PERSONAL) {
            user = UserUtil.getUserInfoLocalBySP();
            User userNet = UserUtil.getUserInfoFromSP();
            if (user == null){
                ToastUtil.showToast(getString(R.string.no_understand_error));
                return;
            }
            if (userNet == null){
                ToastUtil.showToast(getString(R.string.no_understand_error));
                return;
            }
            mPersonalHeader.setImageURI(user.getHeadImg());
            mPersonalBg.setImageURI(user.getBackgroundImg());
            if (user.getHeadImg().length() <= 0){
                mPersonalHeader.setImageURI(Constant.DOWNLOAD_URL + userNet.getHeadImg());
            }
            if (user.getBackgroundImg().length() <= 0){
                mPersonalBg.setImageURI(Constant.DOWNLOAD_URL + userNet.getBackgroundImg());
            }
            mPersonalQrCode.setImageURI(Constant.DOWNLOAD_URL + "image/cache/12.jp");
            mEdPersonalInfo.setText("编辑界面");

        } else if (getIntent().getIntExtra(USER_TYPE, -1) == OTHER) {
            user = otherUser;
            mPersonalHeader.setImageURI(Constant.DOWNLOAD_URL + user.getHeadImg());
            mPersonalBg.setImageURI(Constant.DOWNLOAD_URL + user.getBackgroundImg());
            mPersonalQrCode.setImageURI(Constant.DOWNLOAD_URL + "image/cache/12.jp");
            mEdPersonalInfo.setText("加好友");
        } else {
            ToastUtil.showToast(getString(R.string.no_understand_error));
            return;
        }
        mPersonalName.setText(user.getName());
        mPersonalAccount.setText(user.getAccount());
        mPersonalEmail.setText(user.getEmail());
        if (user.getSex() == 2) {
            mPersonalSexAndAge.setText("性别未知");
        } else if (user.getSex() == 1) {
            mPersonalSexAndAge.setText("女");
        } else {
            mPersonalSexAndAge.setText("男");
        }
        mPersonalLove.setText("旅游");
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_personal_center;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.personal_qr_code:
                //TODO 显示个人二维码
                break;
            case R.id.ed_personal_info:
                if (viewType == PERSONAL) {
                    //TODO 跳转到编辑界面
                } else {
                    //加为好友
                    HttpManager.addFriend(otherUser.getUserId(), UserUtil.getUserInfoFromSP().getUserId());
                }
                break;
        }
    }

    public static void startPersonalCenterActivity(final Activity activity, User user, int type) {
        Intent intent = new Intent(activity, PersonalCenterActivity.class);
        intent.putExtra(USER_INFO, user);
        intent.putExtra(USER_TYPE, type);
        activity.startActivity(intent);
    }

    private void saveAndUpdateUserImageBg(Uri currentPath) {
        User user = null;
        try {
            //更新数据
            user = UserUtil.getUserInfoLocalBySP();
            user.setBackgroundImg(currentPath + "");
            UserUtil.setUserInfoLocalBySP(user);
            User userInfoGson = UserUtil.getUserInfoFromSP();
            userInfoGson.setBackgroundImg("image/user/" + StringUtil.getFileName(user.getBackgroundImg()));
            String userJson = UserUtil.setUserInfoFromSP(userInfoGson);
            HttpManager.updateUserInfo(userJson);
            HttpManager.uploadUserInfo(user.getBackgroundImg());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void getPath(Uri currentPath) {
        mPersonalBg.setImageURI(Uri.parse(currentPath + ""));
        saveAndUpdateUserImageBg(currentPath);
    }
}
